package com.yxsd.web.controller.admin;

import com.pandawork.core.exception.SSException;
import com.pandawork.core.log.LogClerk;
import com.pandawork.core.util.Pagination;
import com.yxsd.common.annotation.Module;
import com.yxsd.common.entity.Product;
import com.yxsd.common.entity.Supplier;
import com.yxsd.common.enums.ModuleEnums;
import com.yxsd.common.utils.WebConstants;
import com.yxsd.web.spring.AbstractController;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * AdminProductController
 *
 * @author: zhangteng
 * @time: 2015/1/4 19:46
 */
@Module(value = ModuleEnums.AdminProduct)
@Controller
@RequestMapping(value = "admin/product")
public class AdminProductController extends AbstractController {

    @Module(value = ModuleEnums.AdminProductList)
    @RequestMapping(value = {"", "list"}, method = RequestMethod.GET)
    public String list(@RequestParam(value = "pageNo", required = false) Integer pageNo,
                       @RequestParam(value = "search", required = false) String search,
                       Model model) {
        try {
            if (pageNo == null) {
                pageNo = 1;
            }
            if (search == null || search.equals("") || search.length() == 0) {
                search = null;
            }
            int dataCount = productService.queryCountByCondition(search);
            Pagination page = new Pagination(dataCount, this.pageSize, pageNo);
            List<Product> productList = productService.queryByCondition(page, search);

            model.addAttribute("productList", productList);
            model.addAttribute("page", page);
            model.addAttribute("search", search);
            return "admin/product/list";
        } catch (SSException e) {
            sendErrMsg(e.getMessage());
            return WebConstants.sysErrorCode;
        }
    }

    @Module(value = ModuleEnums.AdminProductNew)
    @RequestMapping(value = "new", method = RequestMethod.GET)
    public String toInsert(Model model) {
        try {
            List<Supplier> supplierList = supplierService.queryAll();
            model.addAttribute("supplierList", supplierList);
        } catch (SSException e) {
            LogClerk.errLog.error(e);
            sendErrMsg(e.getMessage());
            return WebConstants.sysErrorCode;
        }
        return "admin/product/new";
    }

    @Module(value = ModuleEnums.AdminProductNew)
    @RequestMapping(value = "", method = RequestMethod.POST)
    public String insert(Product product) {
        try {
            productService.insert(product);
        } catch (SSException e) {
            LogClerk.errLog.error(e);
            sendErrMsg(e.getMessage());
            return WebConstants.sysErrorCode;
        }
        return "redirect:/admin/product";
    }

    @Module(value = ModuleEnums.AdminProductEdit)
    @RequestMapping(value = "update/{id}", method = RequestMethod.GET)
    public String toUpdate(@PathVariable("id") Integer id,
                           Model model) {
        try {
            Product product = productService.queryById(Product.class, id);
            List<Supplier> supplierList = supplierService.queryAll();
            model.addAttribute("supplierList", supplierList);
            model.addAttribute("product", product);
        } catch (SSException e) {
            LogClerk.errLog.error(e);
            sendErrMsg(e.getMessage());
            return WebConstants.sysErrorCode;
        }
        return "admin/product/edit";
    }

    @Module(value = ModuleEnums.AdminProductEdit)
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public String update(Product product) {
        try {
            productService.update(product);
        } catch (SSException e) {
            LogClerk.errLog.error(e);
            sendErrMsg(e.getMessage());
            return WebConstants.sysErrorCode;
        }
        return "redirect:/admin/product";
    }

    @RequestMapping(value = "del", method = RequestMethod.GET)
    public
    @ResponseBody
    JSONObject del(@RequestParam("id") Integer id) {
        try {
            Product product = new Product();
            product.setId(id);
            productService.delete(product);
        } catch (SSException e) {
            LogClerk.errLog.error(e);
            sendErrMsgAndErrCode(e);
        }
        return sendSuccessMsg();
    }
}
